{{define "login"}}
<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="utf-8"/>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <link rel="stylesheet" href="../../static/layui/css/modules/code.css"/>
        <link rel="stylesheet" href="../../static/layui/css/layui.css" media="all"/>
        <link rel="icon" href="../../static/images/favicon.ico"/>
        <link rel="stylesheet" href="../../static/user/user.css"/>
        <script src="../../static/layui/layui.js"></script>
        <script src="../../static/md5.js"></script>
        <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
        <title>欢迎登录</title>
    </head>
    <body>
        <!--整体容器-->
        <div id="continer">
            <!--登录框-->
            <div id="login_continer">
                <!--标题-->
            <div id="login_title">
                <h2>欢迎使用SwBlog</h2>                
            </div>
              <div>
                  
                    <form class="layui-form" method="POST">
                        <input type="text" name="username" id="username" required lay-verify="required" placeholder="用户名" class="layui-input"/>
                                               
                        <input type="password" name="password" id="password" required lay-verify="required" placeholder="密码" class="layui-input"/>
                        <!--<div class="checkbox-item">
                            <input type="checkbox"  name="chksave" id="chksave"  lay-skin="primary" class="layui-input"/>
                            <label for="chksave">记住密码</label>
                        </div>-->                                       
                        <button id="ckbtn" type="submit" lay-submit lay-filter="login" class="layui-btn layui-btn-normal layui-btn-radius" >登&nbsp;&nbsp;入</button>
                    </form>
               
                </div>
                
            </div>
        </div>
        <script>
            layui.use('form',function(){
                var form=layui.form;
                form.on('submit(login)',function(data){
                    var username=data.field.username
                    var password=hex_md5(data.field.password);
                    //var jsonstr="\"{\"username\":\""+username+"\",\"password\":\""+password+"\"}\"";
                    //var senddata=JSON.parse(jsonstr);
                    var jsondata={
                        "loginname":username,
                        "password":password
                    }
                    var loading = layer.msg('加载中...', {icon: 16, shade: 0.3, time:0});
                    $.ajax({
                        url:'/user/login',
                        type:'post',
                        async:true,
                        timeout:5000,
                        dataType:'json',
                        contentType:'application/json',
                        data:JSON.stringify(jsondata),
                        success:function(data){
                            layer.close(loading);
                            var jsondata=JSON.parse(data);
                            if(jsondata.loginstate.code==0){
                                layer.open({
                                    title:"欢迎回来",
                                    content:"你好，"+jsondata.username+"!",
                                    yes:function(){
                                        //跳转页面
                                        var bgurl='/backstage/index';
                                        window.location.href=bgurl;
                                    }
                                });
                                
                                
                            }else{
                                layer.open({
                                    title:"登录失败",
                                    content:jsondata.loginstate.msg
                                });
                            }   
                        },
                        error:function(){
                            layer.close(loading);
                            layer.open({
                                    title: '服务器无响应',
                                    content: "服务器跑到月球啦！！"
                            });
                        }
                    });
                    return false;
                });
            });
        </script>
    </body>
</html>
{{end}}